package com.bestcem.xm.survey.export.type;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.survey.export.BaseTypeHandler;
import com.bestcem.xm.survey.grpc.client.dto.QstructsDto;
import org.springframework.stereotype.Component;

import java.util.*;

import static com.bestcem.xm.survey.enums.QuestionTypeEnum.QUESTION_TYPE_MATRIX_BLANK;

@Component
public class MatrixBlankHandler implements BaseTypeHandler {

    @Override
    public Map<String, String> getOpenColumnChangeDictV2(JSONObject data) {
        Map<String, String> res = new HashMap();
        List<JSONObject> rows_items = (List<JSONObject>) data.getOrDefault("rows_items", Collections.emptyList());
        List<JSONObject> items = (List<JSONObject>) data.getOrDefault("items", Collections.emptyList());
        rows_items.forEach(v -> {
            if (isOpenRow(v)) {
                res.put(getOpenRealcolV2(data, v, null), getOpenReadablecolV2(data, v, null));
            }
            items.forEach(s -> res.put(getOpenRealcolV2(data, v, s), getOpenReadablecolV2(data, v, s)));
        });
        return res;
    }

    private String getOpenRealcolV2(JSONObject data, JSONObject row, JSONObject item) {
        if (Objects.isNull(item)) {
            return String.format("%s__%s__row__open", data.getString("cid"), row.getString("oid"));
        }
        return String.format("%s__%s__%s", data.getString("cid"), row.getString("oid"), item.getString("oid"));
    }

    private String getOpenReadablecolV2(JSONObject data, JSONObject row, JSONObject item) {
        //根据self.content_type对命名进行处理
        if (Objects.isNull(item)) {
            return String.format("%s__%s__开放", BaseTypeHandler.clearHtml(data.getString("title")), BaseTypeHandler.clearHtml(row.getString("title")));
        }
        return String.format("%s__%s__%s", BaseTypeHandler.clearHtml(data.getString("title")), BaseTypeHandler.clearHtml(row.getString("title")), BaseTypeHandler.clearHtml(item.getString("title")));
    }

    @Override
    public List<String> getOpenColumnListV2(JSONObject data) {
        List<String> res = new ArrayList();
        List<JSONObject> rows_items = (List<JSONObject>) data.getOrDefault("rows_items", Collections.emptyList());
        List<JSONObject> items = (List<JSONObject>) data.getOrDefault("items", Collections.emptyList());
        rows_items.forEach(v -> {
            if (isOpenRow(v)) {
                res.add(getOpenRealcolV2(data, v, null));
            }
            items.forEach(s -> res.add(getOpenRealcolV2(data, v, s)));
        });
        return res;
    }

    @Override
    public Map<Object, Object> getOpenAnswerDictV2(JSONObject dataValue, Object answerValue) {
        Map<Object, Object> data = new HashMap<>();
        List<JSONObject> items = (List<JSONObject>) dataValue.getOrDefault("items", Collections.emptyList());
        List<JSONObject> row_items = (List<JSONObject>) dataValue.getOrDefault("rows_items", Collections.emptyList());
        if (Objects.isNull(answerValue)) {
            row_items.forEach(row_item -> {
                if (isOpenOption(row_item)) {
                    data.put(getOpenRealcolV2(dataValue, row_item, null), null);
                }
                items.forEach(item -> data.put(getOpenRealcolV2(dataValue, row_item, item), null));
            });
        } else {
            Map<String, Object> answers = (Map<String, Object>) answerValue;
            row_items.forEach(row_item -> {
                if (isOpenOption(row_item)) {
                    answers.forEach((row_gid, answer) -> {
                        if (row_gid.equals(String.format("%s_open", row_item.get("gid")))) {
                            data.put(getOpenRealcolV2(dataValue, row_item, null), BaseTypeHandler.clearHtml((String) answer));
                        }
                    });
                }
                answers.forEach((row_gid, answer) -> {
                    if (row_gid.equals(row_item.getString("gid"))) {
                        items.forEach(item -> data.put(getOpenRealcolV2(dataValue, row_item, item), BaseTypeHandler.clearHtml((String) ((Map) answer).getOrDefault(item.getOrDefault("gid", ""), ""))));
                    }
                });
            });
        }
        return data;
    }

    @Override
    public List<List<String>> getVarLabelsV2(JSONObject dataValue, boolean not01, boolean need_title) {
        List<List<String>> data = new ArrayList<>();
        List var_list = new ArrayList(), val_list = new ArrayList();
        List<JSONObject> rows_items = (List<JSONObject>) dataValue.getOrDefault("rows_items", Collections.emptyList());
        List<JSONObject> items = (List<JSONObject>) dataValue.getOrDefault("items", Collections.emptyList());
        rows_items.forEach(row ->
                items.forEach(item ->
                        var_list.add(String.format("VARIABLE LABELS %s__%s__%s '%s'.", dataValue.get("cid"), row.get("oid"), item.get("oid"), getTitles(dataValue, row, null, false)))));
        data.add(var_list);
        data.add(val_list);
        return data;
    }

    @Override
    public List<String> getSpssCodeV2(JSONObject param, QstructsDto qstruct) {
        List<JSONObject> items = (List<JSONObject>) param.getOrDefault("items", Collections.emptyList());
        List<JSONObject> rows_items = (List<JSONObject>) param.getOrDefault("rows_items", Collections.emptyList());
        List<String> head_list = new ArrayList<>();
        rows_items.forEach(row -> {
            items.stream().forEach(item -> {
                head_list.add(String.format("%s__%s__%s", param.get("cid"), row.get("oid"), item.get("oid")));
                if (item.getBoolean("is_open") && item.getBoolean("is_open")) {
                    head_list.add(String.format("%s__%s__row_open", param.get("cid"), row.get("oid")));
                }
            });
        });
        return head_list;
    }

    @Override
    public String getQuestionTypeValue() {
        return QUESTION_TYPE_MATRIX_BLANK.getValue();
    }
}